package com.example.springsecurityoauthdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author YCL
 * @version 1.0
 * @Description
 * @date 2021/10/14
 */

/**
 * WebSecurity用来创建FilterChainProxy过滤器，
 * HttpSecurity用来创建过滤器链的每个元素
 *
 *
 *
 */
@Configuration
//@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder getPasswordEncoder(){
       // return new BCryptPasswordEncoder();
        return NoOpPasswordEncoder.getInstance();
    }

//    @Autowired
//    private UserDetailsService userDetailsService;

    @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("zhangsan").password("123").authorities("update").build());
        manager.createUser(User.withUsername("lisi").password("456").authorities("index").build());
        return manager;
    }



//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/user/login");
//    }
//
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        //配置没有权限跳转的自定义页面
//        http.exceptionHandling().accessDeniedPage("/unauth.html");
//        http.logout().logoutUrl("/logout").logoutSuccessUrl("/hello").permitAll();
//
//        http.formLogin()
//                .loginPage("/login.html")
//                .loginProcessingUrl("/user/login")
//                .defaultSuccessUrl("/success.html").permitAll()
//                .and()
//                .authorizeRequests()
//                .antMatchers("/","/user/login").permitAll()
//                .antMatchers("/index").hasAuthority("admin")
//                .antMatchers("/test").hasAuthority("admin123")
//                .anyRequest().authenticated()
//                .and()
//                .csrf().disable();
        http.csrf().disable()
                .authorizeRequests()
//                .antMatchers("/r/r1").hasAuthority("p1")
//                .antMatchers("/r/r2").hasAuthority("p2")
                .antMatchers("/r/**").authenticated()
                .anyRequest().permitAll()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/user/login")
                .successForwardUrl("/login-success")
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
        ;
    }

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
////        String pwd = "123456";
////        PasswordEncoder passwordEncoder = getPasswordEncoder();
////        pwd = passwordEncoder.encode(pwd);
////        auth.inMemoryAuthentication().withUser("admin").password(pwd).roles("admin");
//        auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
//    }

    public static void main(String[] args) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("123456");
        System.out.println("encode = " + encode);
    }


}
